package com.cmwa.solution.business.product.service.impl;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.springframework.stereotype.Service;

import com.cmwa.solution.base.entity.Employee;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.product.dao.SubPrdLineDao;
import com.cmwa.solution.business.product.entity.SubPrdLineVo;
import com.cmwa.solution.business.product.service.SubPrdLineService;
import com.cmwa.solution.sys.attach.dao.AttachDao;
import com.cmwa.solution.sys.attach.service.AttachService;
import com.cmwa.solution.utils.SessionUtils;

/**
 * 招商财富产品表业务实现层
 * 
 * @author ex-zhengqq
 * @date 2019-01-22 15:33:47
 */
@Service
public class SubPrdLineServiceImpl implements SubPrdLineService {
	@Resource
	private SubPrdLineDao subPrdLineDao;
	
	@Resource
	private AttachDao attachDao;
	
	@Resource
	private AttachService attachService;
	
	public void insertPrdInfo(SubPrdLineVo prdInfo) {
		subPrdLineDao.insertPrdInfo(prdInfo);
	}
	
	public void physicDeletePrdInfoById(String id) {
		subPrdLineDao.physicDeletePrdInfoById(id);
	}
	
	
	public void updatePrdInfoById(SubPrdLineVo prdInfo) {
		subPrdLineDao.updatePrdInfoById(prdInfo);
	}
	
	public SubPrdLineVo getPrdInfoByCode(String subPrdCode) {
		return subPrdLineDao.getPrdInfoByCode(subPrdCode);
	}
	
	public Page<SubPrdLineVo> prdInfoListPage(SearchParam sp) {
		transferData(sp,"prdCategorys");
		transferData(sp,"prdTerms");
		transferData(sp,"riskLevels");
		List<SubPrdLineVo> items = subPrdLineDao.prdInfoListPage(sp);
		return Page.create(items, sp.getStart(), sp.getLimit(), sp.getTotal());
	}
	
	@Override
	public List<SubPrdLineVo> queryPrdInfoList(String prdsname){
		return subPrdLineDao.queryPrdInfoList(prdsname);
	}
	
	/**
	 * 数据转换
	 * @param sp
	 * @param type
	 */
	private void transferData(SearchParam sp,String type) {
		String types = (String)sp.getSp().get(type);
		if (StringUtils.isNotBlank(types)) {
			sp.getSp().put(type, types.split(","));
		}else {
			sp.getSp().put(type, null);
		}
	}

	@Override
	public void export(HttpServletResponse response, SearchParam sp) throws IOException {
		transferData(sp,"prdTypes");
		transferData(sp,"prdTerms");
		transferData(sp,"riskLevels");
		List<SubPrdLineVo> list = subPrdLineDao.prdInfoList(sp);
		exportData(response,list);
	}
	
	public void exportData(HttpServletResponse response,
			List<SubPrdLineVo> list) throws IOException{
		
		String curDateStr = new SimpleDateFormat("yyyyMMddHHmm").format(new Date());

		HSSFWorkbook wb = new HSSFWorkbook();
		
		// 样式
		HSSFCellStyle titleStyle = wb.createCellStyle();
		//titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
		HSSFFont titleFont = wb.createFont(); // 创建字体
		titleFont.setFontName("宋体"); // 黑体
		titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 加粗
		titleStyle.setFont(titleFont);
		HSSFSheet sheet = wb.createSheet("产品信息");
		
		sheet.setColumnWidth(0, 20 * 256);
		sheet.setColumnWidth(1, 20 * 256);
		sheet.setColumnWidth(2, 20 * 256);
		sheet.setColumnWidth(3, 20 * 256);
		sheet.setColumnWidth(4, 20 * 256);
		sheet.setColumnWidth(5, 20 * 256);

		HSSFRow row = sheet.createRow(0);// 生成头部信息
		
		String[] titleRow = new String[] {"产品编码", "产品名称","产品类型","期限(年)","规模(元)","风险等级"};

		for (int i = 0; i < titleRow.length; i++) {
			HSSFCell cell = row.createCell(i, Cell.CELL_TYPE_STRING);
			cell.setCellValue(titleRow[i]);
			cell.setCellStyle(titleStyle);
		}

		//基本信息
		for (int i = 0; i < list.size(); i++) {

			SubPrdLineVo dto = list.get(i);
			HSSFRow rowData = sheet.createRow(i + 1);

			HSSFCell cell = null;
			
			cell = rowData.createCell(0, Cell.CELL_TYPE_STRING);
			cell.setCellValue(dto.getSubPrdCode());
			cell = rowData.createCell(1, Cell.CELL_TYPE_STRING);
			cell.setCellValue(dto.getSubFundSname());
			cell = rowData.createCell(2, Cell.CELL_TYPE_STRING);
			cell.setCellValue(dto.getPrdCategoryNM());
			cell = rowData.createCell(3, Cell.CELL_TYPE_STRING);
			cell.setCellValue(dto.getPrdTerm());
			cell = rowData.createCell(4, Cell.CELL_TYPE_STRING);
			cell.setCellValue(dto.getExpectedRaiseAmt());
			cell = rowData.createCell(5, Cell.CELL_TYPE_STRING);
			cell.setCellValue(dto.getPrdRiskLevelMN());
			
		}
		// 相关的响应信息
		response.setContentType("application/vnd.ms-excel;charset=UTF-8");
		String fileName = new String("产品信息导出".getBytes("GBK"), "ISO8859-1");
		response.setHeader("Content-Disposition", "Attachment;filename="
				+ fileName + "(" + curDateStr + ").xls");
		ServletOutputStream output = response.getOutputStream();
		wb.write(output);
		if (output != null) {
			output.close();
		}
	
		
	}

	@Override
	public void updateAttachRel(SubPrdLineVo subPrdLineVo, String attSource) {
		// 删除之前的关联关系
		attachDao.deleteAttachRel2(subPrdLineVo.getSubPrdCode(), attSource);
		
		String empId = SessionUtils.getEmployee().getID();
		if(StringUtils.isNotBlank(subPrdLineVo.getAttIds())) {
			attachDao.addAttachRel2(empId, subPrdLineVo.getSubPrdCode(), 
					attSource, subPrdLineVo.getAttIds());
			
		}
	}

	@Override
	public List<SubPrdLineVo> getSellWellPrd(int size) {
		
		return subPrdLineDao.getSellWellPrd(size);
	}

	@Override
	public void savePrdInfo(SubPrdLineVo subPrdLineVo, String attIds) {
		// 文本转成数字
		convertDigital(subPrdLineVo);
		Employee employee = SessionUtils.getEmployee();
		subPrdLineVo.setCreateId(employee.getID());
		subPrdLineDao.insertPrdInfo(subPrdLineVo);
		if (StringUtils.isNotEmpty(attIds)) {
			attachService.addAttachRel2(employee.getID(), subPrdLineVo.getSubPrdCode(), "PRD_LINE", attIds);
		}
	}

	@Override
	public void deletePrdLineByCode(String subPrdCode) {
		subPrdLineDao.logicDelPrdInfoByCode(subPrdCode);
	}

	@Override
	public void updatePrdInfo(SubPrdLineVo prdInfoVo, String attIds) {
		// 文本转成数字
		convertDigital(prdInfoVo);
		Employee employee = SessionUtils.getEmployee();
		prdInfoVo.setModifyId(employee.getID());
		subPrdLineDao.updatePrdInfo(prdInfoVo);
		attachService.deleteAttachRel2(prdInfoVo.getSubPrdCode(), "PRD_LINE");
		if (StringUtils.isNotEmpty(attIds)) {
			attachService.addAttachRel2(employee.getID(), prdInfoVo.getSubPrdCode(), "PRD_LINE", attIds);
		}
	}
	


	@Override
	public List<SubPrdLineVo> prdLineInfo(SearchParam sp) {
		return subPrdLineDao.prdLineInfo(sp) ;
	}

	


	@Override
	public boolean isRepeat(String subPrdCode, String subFundSname) {
		int count = subPrdLineDao.getCount(subPrdCode,subFundSname);
		return count > 0;
	}
	
	private void convertDigital(SubPrdLineVo prdInfoVo) {
		if(prdInfoVo.getPrdTerm() != null) {
			prdInfoVo.setPrdTerm(prdInfoVo.getPrdTerm().replaceAll(",", ""));		
		}
		if(prdInfoVo.getExpectedRaiseAmt() != null) {
			prdInfoVo.setExpectedRaiseAmt(prdInfoVo.getExpectedRaiseAmt().replaceAll(",", ""));
		
		}
		if(prdInfoVo.getInitParValue() != null) {
			prdInfoVo.setInitParValue(prdInfoVo.getInitParValue().replaceAll(",", ""));
		}
		
		if(prdInfoVo.getIndBookStart() != null) {
			prdInfoVo.setIndBookStart(prdInfoVo.getIndBookStart().replaceAll(",", ""));
		}
		
		if(prdInfoVo.getInsBookStart() != null) {
			prdInfoVo.setInsBookStart(prdInfoVo.getInsBookStart().replaceAll(",", ""));
		}
		
		if(prdInfoVo.getInsBookStart() != null) {
			prdInfoVo.setInsBookStart(prdInfoVo.getInsBookStart().replaceAll(",", ""));
		}
		
		if(prdInfoVo.getBreachFeeRate() != null) {
			prdInfoVo.setBreachFeeRate(prdInfoVo.getBreachFeeRate().replaceAll(",", ""));
		}
		
		if(prdInfoVo.getBookFeeRate() != null) {
			prdInfoVo.setBookFeeRate(prdInfoVo.getBookFeeRate().replaceAll(",", ""));
		}
		
		if(prdInfoVo.getDeclareFeeRate() != null) {
			prdInfoVo.setDeclareFeeRate(prdInfoVo.getDeclareFeeRate().replaceAll(",", ""));
		}
		
		if(prdInfoVo.getRedeemFeeRate() != null) {
			prdInfoVo.setRedeemFeeRate(prdInfoVo.getRedeemFeeRate().replaceAll(",", ""));
		}
		
	}
}
